//
//  Async.m
//  Async
//
//  Created by world on 13-6-5.
//  Copyright (c) 2013年 world. All rights reserved.
//

#import "Async.h"

@implementation Async
#ifndef ASYNC_ONE_PARAMETER_NUMBER_OF_ARGUMENTS
#define ASYNC_ONE_PARAMETER_NUMBER_OF_ARGUMENTS  3
#endif


+(AsyncTask*)asyncTask:(void(^)(void)) task{
    id (^wrapperTask)(void)  = (id)^(void){ task(); return nil;};
    return [Async asyncTask:wrapperTask withCallBack:nil onTarget:nil];
}

+(AsyncTask*)asyncTask:(id(^)(void)) task withCallBack:(SEL) callBack onTarget:(id) target {
    if (target) {
        if (![target respondsToSelector:callBack]) {
            [NSException raise:@"unrecognized selector %@ on target %@" format: NSStringFromSelector(callBack), target, nil];
        }
        NSMethodSignature* signature = [target methodSignatureForSelector:callBack];
        if([signature numberOfArguments] > ASYNC_ONE_PARAMETER_NUMBER_OF_ARGUMENTS){
            [NSException raise:@"selector %@ on target %@ max have one parameter" format: NSStringFromSelector(callBack), target, nil];
        }
    }
    AsyncTask* asyncTask = [[AsyncTask alloc] init];
    asyncTask.task = task;
    asyncTask.callback = callBack;
    asyncTask.target = target;
    [[Async sharedQuere] addOperation:asyncTask];
    return asyncTask;
}




/**add plist config */
+(NSOperationQueue*) sharedQuere{
    static dispatch_once_t pred = 0;
    static NSOperationQueue* queue = nil;
    dispatch_once(&pred, ^{
        queue = [[NSOperationQueue alloc] init];
        [queue setMaxConcurrentOperationCount:6];
        [queue setName:@"Async Task Queue"];
    });
    return queue;
}



@end
